package cmbchina.util.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by zm on 17-11-12.
 */
public class FlowFormDb {

    public static int updateFormInfo(String from_info,String task_definition_key,String process_inst_id) {
        DBHelper dbHelper = new DBHelper();
        Connection conn = dbHelper.getConn();
        int i = 0;
        String sql = "update cus_flow_form o set o.from_info = ?  where process_inst_id = ? and o.task_definition_key = ?";
        PreparedStatement pstmt = null;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1,from_info);
            pstmt.setString(2,process_inst_id);
            pstmt.setString(3,task_definition_key);
            i = pstmt.executeUpdate();
            System.out.println("resutl=updateFormInfo==: update cus_flow_form o set o.from_info = "+from_info+"  where process_inst_id = "+process_inst_id+" and o.task_definition_key = "+task_definition_key );

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbHelper.close(conn,pstmt,null);
        }
        return i;
    }

    public static int updateTaskSate(String runState,String task_definition_key,String process_inst_id) {
        DBHelper dbHelper = new DBHelper();
        Connection conn = dbHelper.getConn();
        int i = 0;
        String sql = "update cus_flow_form o set o.result_state = ?  where process_inst_id = ? and o.task_definition_key = ?";
        PreparedStatement pstmt = null;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1,runState);
            pstmt.setString(2,process_inst_id);
            pstmt.setString(3,task_definition_key);
            i = pstmt.executeUpdate();
            System.out.println("resutl=updateFormInfo==: update cus_flow_form o set o.from_info = "+runState+"  where process_inst_id = "+process_inst_id+" and o.task_definition_key = "+task_definition_key );

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbHelper.close(conn,pstmt,null);
        }
        return i;
    }

    public static String queryFormInfo(String task_definition_key,String process_inst_id) {
        DBHelper dbHelper = new DBHelper();
        Connection conn = dbHelper.getConn();
        int i = 0;
        String formInfo = "";
        ResultSet ret = null;
        String sql = "select from_info from cus_flow_form  where process_inst_id = ? and task_definition_key = ?";
        PreparedStatement pstmt = null;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1,process_inst_id);
            pstmt.setString(2,task_definition_key);
            ret = pstmt.executeQuery();
            while (ret.next()) {
                formInfo = ret.getString(1);
            }
            System.out.println("elect form_info from cus_flow_form  where process_inst_id= "+process_inst_id+" and o.task_definition_key = "+task_definition_key );

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbHelper.close(conn,pstmt,ret);
        }
        return formInfo;
    }

    public static String queryTaskType(String task_definition_key,String process_inst_id) {
        DBHelper dbHelper = new DBHelper();
        Connection conn = dbHelper.getConn();
        int i = 0;
        String task_type = "";
        ResultSet ret = null;
        String sql = "select task_type from cus_flow_form  where process_inst_id = ? and task_definition_key = ?";
        PreparedStatement pstmt = null;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1,process_inst_id);
            pstmt.setString(2,task_definition_key);
            ret = pstmt.executeQuery();
            while (ret.next()) {
                task_type = ret.getString(1);
            }
            System.out.println("elect task_type from cus_flow_form  where process_inst_id= "+process_inst_id+" and o.task_definition_key = "+task_definition_key );

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbHelper.close(conn,pstmt,ret);
        }
        return task_type;
    }


    public static String queryFormInfoByTaskType(String task_type,String process_inst_id) {
        DBHelper dbHelper = new DBHelper();
        Connection conn = dbHelper.getConn();
        int i = 0;
        String formInfo = "";
        ResultSet ret = null;
        String sql = "select from_info from cus_flow_form  where process_inst_id = ? and task_type = ?";
        PreparedStatement pstmt = null;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1,process_inst_id);
            pstmt.setString(2,task_type);
            ret = pstmt.executeQuery();
            while (ret.next()) {
                formInfo = ret.getString(1);
            }
            System.out.println("elect form_info from cus_flow_form  where process_inst_id= "+process_inst_id+" and o.task_type = "+task_type );

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbHelper.close(conn,pstmt,ret);
        }
        return formInfo;
    }

    /**
     * 判断参数是否已经全部准备ok
     * @param process_inst_id
     * @return
     */
    public static boolean flowIsOk(String process_inst_id) {
        DBHelper dbHelper = new DBHelper();
        Connection conn = dbHelper.getConn();
        int i = 0;
        String formInfo = "";
        ResultSet ret = null;
        boolean isOk = true;
        String sql = "select from_info from cus_flow_form where from_info is null and   process_inst_id=? and task_type = 'UserTask'";
        PreparedStatement pstmt = null;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1,process_inst_id);
            ret = pstmt.executeQuery();
            while (ret.next()) {
                isOk = false;
            }
            System.out.println("select from_info from cus_flow_form where from_info is null" );

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbHelper.close(conn,pstmt,ret);
        }
        return isOk;
    }

    /**
     * 判断参数是否已经全部准备ok
     * @param process_inst_id
     * @return
     */
    public static boolean isEnd(String process_inst_id) {
        DBHelper dbHelper = new DBHelper();
        Connection conn = dbHelper.getConn();
        int i = 0;
        String formInfo = "";
        ResultSet ret = null;
        boolean isOk = true;
        String sql = "select result_code from cus_flow_form where result_code is null and   process_inst_id=? and task_type like '%Task'";
        PreparedStatement pstmt = null;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1,process_inst_id);
            ret = pstmt.executeQuery();
            while (ret.next()) {
                isOk = false;
            }
            System.out.println("select from_info from cus_flow_form where from_info is null" );

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbHelper.close(conn,pstmt,ret);
        }
        return isOk;
    }


    public static int updateFlowFormState(int state,String process_inst_id,String task_definition_key,String result_code,String result,String scriptCommand) {
        DBHelper dbHelper = new DBHelper();
        Connection conn = dbHelper.getConn();
        int i = 0;

        String sql = "update cus_flow_form  set result_state = ?,result_code = ?,result = ?  where process_inst_id = ? and task_definition_key=?";
        if(scriptCommand!=null&&!"".equals(scriptCommand)){
            sql = "update cus_flow_form  set result_state = ?,result_code = ?,result = ?,script_command = ?  where process_inst_id = ? and task_definition_key=?";
        }
        PreparedStatement pstmt = null;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            if(scriptCommand!=null&&!"".equals(scriptCommand)){
                pstmt.setInt(1,state);
                pstmt.setString(2,result_code);
                pstmt.setString(3,result);
                pstmt.setString(4,scriptCommand);
                pstmt.setString(5,process_inst_id);
                pstmt.setString(6,task_definition_key);
            }else{
                pstmt.setInt(1,state);
                pstmt.setString(2,result_code);
                pstmt.setString(3,result);
                pstmt.setString(4,process_inst_id);
                pstmt.setString(5,task_definition_key);
            }

            i = pstmt.executeUpdate();
            System.out.println("update cus_flow_form  set result_state = "+state+",result_code = "+result_code+",result = "+result+"  where process_inst_id = "+process_inst_id  +"and task_definition_key"+task_definition_key);


            if(state==4||state==5){
                FlowDb.updateFlowState(state, process_inst_id);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbHelper.close(conn,pstmt,null);
        }
        return i;
    }
}
